昨天完成了簡單 aggreagte實作,先試一下event store能不能work:
use book_lib::aggregate::Aggregate;
use book_lib::domain::reader::{Reader, ReaderCommand};
use book_lib::mem_store;
use book_lib::store::EventStore;
#[tokio::main]
async fn main() {
let store = mem_store::MemStore::<Reader>::default();
println!("\n=== 初始 es ===\n{:#?}", store);
// 輸入建立 Reader 的指令
let command = ReaderCommand::CreateReader {
name: "John Smith".to_string(),
};
let id = "test-aggregate-id-C450D1A";
let mut reader = store
.load_aggregate(&id)
.await
.unwrap();
let events = reader.aggregate.handle(command).await.unwrap();
// 將事件寫入 store
store.commit(events, reader, Default::default()).await.unwrap();
println!("\n=== commit 後的 es ===\n{:#?}", store);
// 從event store載入 Reader
let reader = store
.load_aggregate(&id)
.await
.unwrap();
println!("\n=== 從 es 載入 reader ===\n{:#?}", reader)
}
看起來昨天在 Reader
裡實作的id有點多餘,不過沒關係之後沒用到再清理。執行的結果如下:
=== 初始 es ===
MemStore {
events: RwLock {
data: {},
poisoned: false,
..
},
}
=== commit 後的 es ===
MemStore {
events: RwLock {
data: {
"test-aggregate-id-C450D1A": [
EventEnvelope {
aggregate_id: "test-aggregate-id-C450D1A",
sequence: 1,
payload: ReaderCreated {
name: "John Smith",
id: b7dc9868-922d-4762-9791-876e1b01ad60,
},
metadata: {},
},
],
},
poisoned: false,
..
},
}
=== 從 es 載入 reader ===
MemStoreAggregateContext {
aggregate_id: "test-aggregate-id-C450D1A",
aggregate: Reader {
id: b7dc9868-922d-4762-9791-876e1b01ad60,
name: "John Smith",
borrowed_books: [],
},
current_sequence: 1,
}
單純的案例沒有問題,後續再繼續擴充。